﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interfaces;
using Entities;
using System.IO;

namespace FileDao
{
    public class File_Storage_DAO : IStorageDAO
    {
        private string _dir = "/files";

        public void StoreFile(string just_uploaded_path, HostedFile hostedfile)
        {
            if (!Directory.Exists(_dir))
                Directory.CreateDirectory(_dir);
            File.Move(just_uploaded_path, Path.Combine(_dir, hostedfile.ServerSideFileName));
        }

        public void DeleteFile(HostedFile hostedfile)
        {
            File.Delete(Path.Combine(_dir, hostedfile.ServerSideFileName));
        }

        public long GetUsedSpaceValue(User user)
        {
            return DirSize(new DirectoryInfo(_dir));
        }

        public static long DirSize(DirectoryInfo d)
        {
            long Size = 0;
            // Add file sizes.
            FileInfo[] fis = d.GetFiles();
            foreach (FileInfo fi in fis)
            {
                Size += fi.Length;
            }
            // Add subdirectory sizes.
            DirectoryInfo[] dis = d.GetDirectories();
            foreach (DirectoryInfo di in dis)
            {
                Size += DirSize(di);
            }
            return (Size);
        }
    }
}
